package com.lengyu.vxminiprogram.security;

import com.lengyu.vxminiprogram.security.rest.JWTAuthorizationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserModelDetailsService userDetailsService;

    @Autowired
    JwtAccessDeniedHandler jwtAccessDeniedHandler;

//    @Bean
//    public BCryptPasswordEncoder bCryptPasswordEncoder() {
//        return new BCryptPasswordEncoder();
//    }

    @Bean PasswordEnconderNone passwordEnconderNone(){
        return new PasswordEnconderNone();
    }

    @Override

    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)
                .passwordEncoder(passwordEnconderNone());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //对请求进行验证
        http.cors().and().csrf().disable()
                .authorizeRequests()
                // 对本接口的所有请求进行放行
                .antMatchers("/login/**").permitAll()
                //对下面接口进行token验证
                .antMatchers("/**").authenticated()
//                .antMatchers(HttpMethod.POST,"/api/**").authenticated()
//                .antMatchers("/api/message/sendSys").hasAnyAuthority("ROLE_ADMIN")
//                .antMatchers("/api/message/updateSysMsg").hasAnyAuthority("ROLE_ADMIN")
//                .antMatchers("/api/message/getAllSysForGroup").hasAnyAuthority("ROLE_ADMIN")
//                .antMatchers("/api/message/sendGroupSysMsg").hasAnyAuthority("ROLE_ADMIN")
//                .antMatchers("/api/message/sendNotice").hasAnyAuthority("ROLE_ADMIN")
                .anyRequest().permitAll()
                .and()
                .addFilter(new LoginFilter(authenticationManager()))
                .addFilter(new JWTAuthorizationFilter(authenticationManager()))
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)//不需要session
                .and()
                .exceptionHandling().authenticationEntryPoint(new JwtAuthenticationEntryPoint())
                .accessDeniedHandler(jwtAccessDeniedHandler);
    }

    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
        return source;

    }

}
